﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace YasharEl.Infrastructure.ObjectFactory
{
    public class DependencyRegistration
    {
        public Type FromType { get; protected set; }
        public Type ToType { get; protected set; }
        public string Key { get; protected set; }
        public string LifetimeStyle { get; protected set; }

        public DependencyRegistration(Type fromType, Type toType, string lifetimeStyle, string key = null)
        {
            Contract.Requires<ArgumentNullException>(fromType != null, "fromType could not be null.");
            Contract.Requires<ArgumentNullException>(toType != null, "toType could not be null.");
            Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(lifetimeStyle), "lifetimeStyle could not be null or empty.");

            if (!toType.IsAssignableFrom(fromType))
                throw new DependencyRegistrationException(string.Format("To type {0} must be subclass of {1}.", toType.FullName, fromType.FullName));

            FromType = fromType;
            ToType = toType;
            LifetimeStyle = lifetimeStyle;
            Key = key;
        }

        public override string ToString()
        {
            if (!string.IsNullOrEmpty(Key))
            {
                return string.Format("Dependency with name {0} and type {1} registered with '{2}' lifetime style and mapped to {3}.", Key, FromType.FullNameToString(), LifetimeStyle, ToType.FullNameToString());
            }
            else
            {
                return string.Format("Dependency (without name) and with type {0} registered with '{1}' lifetime style and mapped to {2}.", FromType.FullNameToString(), LifetimeStyle, ToType.FullNameToString());
            }
        }
    }
}
